import pymysql
import json

DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'  # 不设置密码，为空
DBNAME = 'Schedule'  # 数据库名字
db = pymysql.connect(host=DBHOST, user=DBUSER, password=DBPASS, database=DBNAME)  # 连接数据库
cur = db.cursor()  # 声明游标 
with open('course.json', 'r', encoding='utf-8') as f:
    course_text = json.load(f)
    val = []
    for each_part in course_text['list']['records']:
        val.append(each_part['kch']+'_'+each_part['kxh'])
        val.append(each_part['kcm'])
        val.append(each_part['skjs'])
        val.append(each_part['xkmssm'])

        val.append(each_part['xf'])
        val.append(each_part['zcsm'])
        val.append(each_part['skxq'])
        val.append(each_part['skjc'])
        val.append(each_part['cxjc'])

        val.append(each_part['xqm'])
        val.append(each_part['jxlm'])
        val.append(each_part['jasm'])
        sql = 'INSERT INTO class2 VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'  # 插入数据
        try:
            cur.execute(sql, val)
            db.commit()
        except:
            db.rollback()
        val.clear()
